package javax.microedition.media;

import javax.microedition.media.Player;
import javax.microedition.media.control.*;
import javax.sound.midi.MetaEventListener;
import javax.sound.midi.MetaMessage;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Vector;

public class PlayerImpl implements Player, Runnable, LineListener, MetaEventListener {
    private Vector PlayerListener;
    public String contentType;
    public int loopCount;
    private long mediaTime;
    public int dataLen;
    Thread thread;
    private int a;
    private boolean b;
    private boolean ab;
    Control toneControl;

    Control volumControl;

    Control midiControl;

    Control[] controls;

    public PlayerImpl(InputStream paramInputStream, String paramString) throws IOException {
        this.contentType = paramString;
        this.loopCount = 1;
        this.dataLen = paramInputStream.available();
        a = 100;

        this.midiControl = (Control) new MIDIControlImpl();
        this.toneControl = (Control) new ToneControlImpl();
        this.volumControl = (Control) new VolumeControlImpl(this);
        this.controls = new Control[] { this.toneControl, this.volumControl, this.midiControl };
        this.PlayerListener=new Vector();
    }

    public int getState() {
        return this.a;
    }

    public void prefetch() throws IllegalStateException, MediaException {
        if (this.a == 0)
            throw new IllegalStateException();
        if (this.a == 100) {
            realize();
        } else if (this.a != 200) {
            return;
        }

        this.a = 300;
    }

    public void realize() throws IllegalStateException, MediaException {
        if (this.a == 0)
            throw new IllegalStateException();
        if (this.a == 100)
            this.a = 200;
    }

    public void setLoopCount(int paramInt) {
        if (paramInt == 0)
            throw new IllegalArgumentException();
        this.loopCount = paramInt;
    }

    public void start() throws IllegalStateException, MediaException {
        if (this.a == 0)
            throw new IllegalStateException();
        if (this.a != 400) {
            // this.thread = new Thread(this);
            // this.thread.start();
            this.a = 400;
            notifyListeners("started", 0L);
        }
    }

    public void run() {
    }

    public void stop() throws IllegalStateException, MediaException {
        if (this.a == 0)
            throw new IllegalStateException();
        if (this.thread != null) {
            Thread clazz = this.thread;
            this.thread = null;
            while (clazz.isAlive())
                ;
        }
    }

    public void addPlayerListener(PlayerListener paramPlayerListener) throws IllegalStateException {
        if (this.PlayerListener == null)
            throw new IllegalStateException();
        if (paramPlayerListener != null)
            this.PlayerListener.add(paramPlayerListener);
    }

    public void removePlayerListener(PlayerListener paramPlayerListener) throws IllegalStateException {
        if (this.PlayerListener == null)
            throw new IllegalStateException();
        if (paramPlayerListener != null)
            this.PlayerListener.remove(paramPlayerListener);
    }

    protected void notifyListeners(String paramString, Object paramObject) {
        Enumeration<PlayerListener> enumeration = this.PlayerListener.elements();
        while (enumeration.hasMoreElements()) {
            PlayerListener playerListener;
            (playerListener = enumeration.nextElement()).playerUpdate(this, paramString, paramObject);
        }
    }

    public void close() {
        if (this.thread != null)
            try {
                stop();
            } catch (Exception exception) {
            }
        // if (this.a instanceof Sequence)
        // this.thread.close();
        // this.thread = null;
        // this.a = false;
        notifyListeners("closed", null);
    }

    public String getContentType() {
        return this.contentType;
    }

    public long getDuration() {
        return 0;
    }

    public long getMediaTime() {
        return mediaTime;
    }

    public long setMediaTime(long paramLong) {
        return paramLong;
    }

    public void deallocate() throws IllegalStateException {
        int i = 200;
        if (this.a == 0) {
            throw new IllegalStateException();
        } else if (this.a == 400) {
            try {
                stop();
            } catch (MediaException e) {
            }
        } else {
            if (this.a != 300) {
                if (this.a == 200) {
                    i = 100;
                } else {
                    return;
                }
            }
            this.a = i;
        }
    }

    public Control getControl(String paramString) {
        return (Control) ((paramString.indexOf("VolumeControl") != -1) ? this.volumControl
                : ((paramString.indexOf("ToneControl") != -1) ? this.toneControl
                        : ((paramString.indexOf("MIDIControl") != -1) ? this.midiControl : null)));
    }

    public Control[] getControls() {
        return (Control[]) this.controls;
    }

    public void update(LineEvent paramLineEvent) {
        if (paramLineEvent.getType() == LineEvent.Type.STOP)
            this.b = true;
    }

    public void meta(MetaMessage paramMetaMessage) {
        if (paramMetaMessage.getType() == 47)
            this.ab = true;
    }
}
